package raf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/*
文件复制，原理：
从原文件中顺序读取每个字节并写入到另一个文件中
 */
public class CopyDemo {
    public static void main(String[] args) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(
                "image.jpg","r"
        );
        RandomAccessFile copy = new RandomAccessFile(
                "image_cp.jpg","rw"
        );
        int d;
        long start = System.currentTimeMillis();
        while((d = raf.read())!=-1){//只要读出来的不是-1，即没到末尾，可以一直循环
            copy.write(d);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制完毕！耗时："+(end-start));
        raf.close();
        copy.close();
    }
}
